import java.util.Iterator;
import java.util.LinkedList;

/*
 * @lc app=leetcode.cn id=705 lang=java
 *
 * [705] 设计哈希集合
 */

// @lc code=start
class MyHashSet {
    //实际上开一个够大的bool[]数组即可
    private static int NUM = 769;//质数，减少冲突。
    private LinkedList<Integer>[] set;

    /** Initialize your data structure here. */
    public MyHashSet() {
        set = new LinkedList[NUM];
        for(int i = 0 ;i<NUM ; i++){
            set[i] = new LinkedList<>();
        }
    }
    
    public void add(int key) {//注意如果存在Key了是不用加的
        int h = key%NUM;
        Iterator<Integer> it = set[h].iterator();
        while(it.hasNext()){
            Integer e = it.next();
            if( e == key){
                return;
            }
        }

        set[h].offerLast(key);//在末尾添加Key
    }
    
    public void remove(int key) {
        int h = key%NUM;
        Iterator<Integer> it = set[h].iterator();
        while(it.hasNext()){
            Integer e = it.next();
            if( e == key){
                set[h].remove(e);//注意，这里用key会报错，用e反而没事，它不会自动包装的么？
                return;
            }
        }
    }
    
    /** Returns true if this set contains the specified element */
    public boolean contains(int key) {
        int h = key%NUM;
        Iterator<Integer> it = set[h].iterator();
        while(it.hasNext()){
            Integer e = it.next();
            if( e == key){
                return true;
            }
        }
        return false;
    }
}

/**
 * Your MyHashSet object will be instantiated and called as such:
 * MyHashSet obj = new MyHashSet();
 * obj.add(key);
 * obj.remove(key);
 * boolean param_3 = obj.contains(key);
 */
// @lc code=end

